package com.clss.commandhelper.function.caseAddInformation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.clss.commandhelper.CommandHelperApplication;
import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class CaseAddInformationActivity extends BaseActivity implements CaseAddInformationPresenter.AddCaseView {

    @BindView(R.id.include_top_bar_title_tv)
    TextView mIncludeTopBarTitleTv;
    @BindView(R.id.task_name)
    EditText mTaskName;
    @BindView(R.id.task_comment)
    EditText mTaskComment;
    @BindView(R.id.task_address)
    EditText mTaskAddress;
    @BindView(R.id.task_sp_type)
    Spinner mTaskSpType;
    @BindView(R.id.task_sp_police_level)
    Spinner mTaskSpPoliceLevel;
    @BindView(R.id.include_top_bar_right_tv)
    TextView mIncludeTopBarRightTv;
    @BindView(R.id.spinner_type_layout)
    LinearLayout mSpinnerTypeLayout;
    @BindView(R.id.spinner_police_level_layout)
    LinearLayout mSpinnerPoliceLevelLayout;

    private AMapLocation mLocation;

    private String id;
    private String areaCode;
    private String policeInfoLat;
    private String policeInfoLng;
    private AddInfoTask mAddInfoTask;

    private CaseAddInformationPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int setViewId() {
        return R.layout.activity_case_add_information;
    }

    @Override
    protected void initView() {
        mLocation = CommandHelperApplication.getMapLocation();
        if (mLocation == null) {
            showShortToast("定位失败，重试");
            finish();
        }
        mPresenter = new CaseAddInformationPresenter(this);

        mIncludeTopBarTitleTv.setText("补充信息");
        mIncludeTopBarRightTv.setVisibility(View.VISIBLE);
        mIncludeTopBarRightTv.setText("提交");
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        areaCode = mLocation.getAdCode();
        policeInfoLat = String.valueOf(mLocation.getLatitude());
        policeInfoLng = String.valueOf(mLocation.getLongitude());
        mTaskAddress.setText(mLocation.getAddress());

    }

    private void commitInformation() {
        if (mTaskName.getText() == null || mTaskName.getText().toString().trim().equals("")) {
            showShortToast("请填写任务名称");
            return;
        }
        if (mTaskAddress.getText() == null || mTaskAddress.getText().toString().trim().equals("")) {
            showShortToast("请填写地址");
            return;
        }
        if (mTaskComment.getText() == null || mTaskComment.getText().toString().trim().equals("")) {
            showShortToast("请填写警情描述");
            return;
        }
        mAddInfoTask = new AddInfoTask();
        mAddInfoTask.setAreaCode(areaCode);
        mAddInfoTask.setId(id);
        mAddInfoTask.setPoliceInfoComment(mTaskComment.getText().toString());
        mAddInfoTask.setPoliceInfoLat(policeInfoLat);
        mAddInfoTask.setPoliceInfoLng(policeInfoLng);
        mAddInfoTask.setPoliceInfoLevel(mTaskSpPoliceLevel.getSelectedItemPosition() + 1);
        mAddInfoTask.setPoliceInfoType(mTaskSpType.getSelectedItemPosition() + 1);
        mAddInfoTask.setTaskName(mTaskName.getText().toString());
        mAddInfoTask.setPoliceInfoAddress(mTaskAddress.getText().toString());

        mPresenter.addCaseInformation();
    }

    @Override
    public void responseError(int code, String msg) {
        dismissLoadingDialog();
        showShortToast(msg);
    }

    @OnClick({R.id.include_top_bar_left_iv, R.id.include_top_bar_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.include_top_bar_left_iv:
                finish();
                break;
            case R.id.include_top_bar_right_tv:
                commitInformation();
                break;
        }
    }


    @Override
    public void addCaseSuccess() {
        dismissLoadingDialog();
        showShortToast("补充信息成功");
        finish();
    }

    @Override
    public AddInfoTask getAddInfoTask() {
        return mAddInfoTask;
    }
}
